package main

import (
	"fmt"
	"gameserver/tools/TestRobot/internal"
	"os/signal"
	"path"
	"strings"
	"syscall"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/global"
	"gitee.com/night-tc/gobige/logger"
	"gitee.com/night-tc/gobige/threads"
	"gitee.com/zhenghexcs/flags"
	"github.com/spf13/viper"
)

func main() {
	flags.SetFlag(common.ServerName, strings.ToUpper(common.ServerName), "服务器名字，表示server.json中的名字", "Robot")
	flags.SetFlag(common.ServerJson, strings.ToUpper(common.ServerJson), "服务器配置文件路径，相对与bin目录", path.Join(common.GetResPath(), "config", "server.json"))
	flags.Parse()
	flags.LoadStruct(internal.Config)

	vmap := viper.New()
	fmt.Println(flags.GetFlagByString(common.ServerJson))
	vmap.SetConfigFile(flags.GetFlagByString(common.ServerJson))
	if err := vmap.ReadInConfig(); err != nil {
		panic(fmt.Errorf("加载配置文件失败 %v", err))
	}
	logger.Init(path.Join(common.GetResPath(), "config", "log.toml"), flags.GetFlagByString(common.ServerName), threads.NewThreadGo())
	defer logger.Flush()
	sinfo := global.LoadServerConfig(vmap, flags.GetFlagByString(common.ServerName), global.ServerTypeDefault)
	global.SetCfgInst(sinfo)
	internal.LoadName()

	logger.Info("[Server] Start.")

	signal.Notify(internal.SvrStop, syscall.SIGINT, syscall.SIGTERM)
	SendMain()
	logger.Info("[Server] Stop.")
	internal.Thgo.CloseWait()
}
